import { ProTableColumn } from '@/components/pro-table/type'
import { NButton, NPopconfirm } from 'naive-ui'
import { BudgetModel } from '@/api/model/budget-model'

type Props = {
  handleEditBudget: (row: BudgetModel, type: 'edit' | 'check') => void
  fetchDelBudget: (id: number) => void
}

export const useBudgetProTable = ({
  handleEditBudget,
  fetchDelBudget
}: Props) => {
  const columns: ProTableColumn<BudgetModel>[] = [
    {
      title: '预算编号',
      key: 'budgetCode',
      query: {
        type: 'input'
      }
    },
    {
      title: '预算名称',
      key: 'budgetName',
      query: {
        type: 'input'
      }
    },
    {
      title: '预算金额',
      key: 'budgetAmount'
    },
    {
      title: '申请人',
      key: 'receivePerson',
      query: {
        type: 'input'
      }
    },
    {
      title: '申请部门',
      key: 'receiveBranch',
      query: {
        type: 'input'
      }
    },
    {
      title: '申请日期',
      key: 'receiveDate'
    },
    {
      title: '申请原因',
      key: 'receiveReason'
    },
    {
      title: '备注',
      key: 'remark'
    },
    {
      type: 'tag',
      title: '申请状态',
      key: 'receiveType',
      dictOptions: useDictData('STATUS').value,
      query: {
        type: 'select',
        dictOptions: useDictData('STATUS').value
      }
    },
    {
      title: '操作',
      key: 'operation',
      render(row) {
        return (
          <>
            {row.receiveType !== '2' ? (
              <NButton
                type="primary"
                size="small"
                onClick={() => handleEditBudget(row, 'edit')}
                quaternary
              >
                修改
              </NButton>
            ) : (
              ''
            )}

            {row.receiveType === '2' ? (
              <NButton
                type="primary"
                size="small"
                onClick={() => handleEditBudget(row, 'check')}
                quaternary
              >
                详情
              </NButton>
            ) : (
              ''
            )}
            {row.receiveType !== '2' ? (
              <NPopconfirm onPositiveClick={() => fetchDelBudget(row.id)}>
                {{
                  trigger: () => (
                    <NButton size="small" type="error" quaternary>
                      删除
                    </NButton>
                  ),
                  default: () => '您确定要删除？'
                }}
              </NPopconfirm>
            ) : (
              ''
            )}
          </>
        )
      }
    }
  ]

  return {
    columns
  }
}
